package main

import (
	"flag"
	"github.com/spf13/viper"
	"go.didapinche.com/boot"
	"go.didapinche.com/foundation/plat-echo/app/ops/crons"
	ginstart "go.didapinche.com/gin-start"
	"go.uber.org/zap"
)

var cf = flag.String("c", "configs/ops.yaml", "set config file which viper will loading.")

func main() {
	flag.Parse()

	app, err := CreateApp(*cf)
	if err != nil {
		panic(err)
	}

	// 启动并等待退出
	app.Start().Await()
}

// NewStart 返回go-boot框架初始化函数.如果存在zues服务请使用下面注释掉掉启动函数
func NewStart(logger *zap.Logger, httpserver *ginstart.Server, scheduler *crons.Scheduler, v *viper.Viper) boot.Start {
	return func() error {
		logger.Info("application is starting...")

		// 启动httpServer
		if err := httpserver.Start(); err != nil {
			panic(err)
		}
		go func() { scheduler.Start() }()

		logger.Info("application is successfully started")
		return nil
	}
}

// NewShutdown 返回go-boot框架shutdown hook.
func NewShutdown(logger *zap.Logger, httpserver *ginstart.Server, scheduler *crons.Scheduler) boot.Shutdown {
	return func() error {
		logger.Info("application is stopping...")

		// 停止httpServer
		if err := httpserver.Stop(); err != nil {
			panic(err)
		}
		go func() { scheduler.Stop() }()
		logger.Info("application is successfully stopped")
		return nil
	}
}

//// NewStart 返回go-boot框架初始化函数.
//func NewStart(logger *zap.Logger, httpserver *ginstart.Server, zeusserver *server.Server, v *viper.Viper) boot.Start {
//	return func() error {
//		logger.Info("application is starting...")
//
//		// 启动httpServer
//		if err := httpserver.Start(); err != nil {
//			panic(err)
//		}
//
//		// 停止zeus服务器
//		zeusserver.Start()
//
//		logger.Info("application is successfully started")
//		return nil
//	}
//}
//
//// NewShutdown 返回go-boot框架shutdown hook.
//func NewShutdown(logger *zap.Logger, httpserver *ginstart.Server, zeusserver *server.Server) boot.Shutdown {
//	return func() error {
//		logger.Info("application is stopping...")
//
//		// 停止httpServer
//		if err := httpserver.Stop(); err != nil {
//			panic(err)
//		}
//
//		zeusserver.Shutdown()
//		logger.Info("application is successfully stopped")
//		return nil
//	}
//}
